//
//  BrandDetailViewController.m
//  HomePage
//
//  Created by xiong qi on 13-5-9.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "BrandDetailViewController.h"

@interface BrandDetailViewController ()

@end

@implementation BrandDetailViewController

-(id)initWithFrame:(CGRect)frame Object:(NSObject*)obj
{
    self = [super init];
    if (self) {
        self.view.frame = frame;
        NSDictionary * dic = (NSDictionary *)obj;
        
        //get brandname
        NSString * title = [dic objectForKey:@"Title"];
        NSArray * titleArray = [title componentsSeparatedByString:@" "];
        NSString * brandName = @"";
        if (titleArray) {
            if ([titleArray count]>0) {
                brandName = [titleArray objectAtIndex:0];
            }
        }
        
        UIImageView * background = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,1024, 768)];
        [self.view addSubview:background];
        [background release];
        [background setImage:[UIImage imageNamed:@"AllAuto_Image_BG"]];
        
        UIButton *  backBtn = [[UIButton alloc]initWithFrame:CGRectMake(40, 20, 45,45)];
        [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [backBtn addTarget:self action:@selector(onBack:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:backBtn];
        [backBtn release];
        
        UIButton *  homeBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, 20, 55,40)];
        [homeBtn setImage:[UIImage imageNamed:@"home_button"] forState:UIControlStateNormal];
        [homeBtn addTarget:self action:@selector(onHome:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:homeBtn];
        [homeBtn release];
        
        
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(198, 25, 400, 35)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.font = [UIFont fontWithName:@"Arial" size:30];
        titleLabel.textColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0];
        [self.view addSubview:titleLabel];
        [titleLabel release];
        [titleLabel setText: [NSString stringWithFormat:@"品牌故事-%@",brandName]];
        
        UIImageView * lineView = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width-658)/2,70, 658, 8)];
        lineView.image = [UIImage imageNamed:@"AllAuto_Config_TimeLine_BG"];
        [self.view addSubview:lineView];
        [lineView release];
        
        /*
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path = [NSString stringWithFormat:@"%@/paddata/company/Brand/%@",[paths objectAtIndex:0],[dic objectForKey:@"DocumentName"]];
        
        brandDetailView = [[BrandDetailView alloc] initWithFrame:CGRectMake(0, 110, frame.size.width, frame.size.height-110) Root:path];
        [self.view addSubview:brandDetailView];
        [brandDetailView release];
         */
        
        UIView * logoview = [[UIView alloc] initWithFrame:CGRectMake(460+103, 89, 820, 25)];
        [self.view addSubview:logoview];
        [logoview release];
        
        imageLogo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 6, 12, 13)];
        [logoview addSubview:imageLogo];
        [imageLogo release];
        imageLogo.image = [UIImage imageNamed:@"Redsanjiao"];
        
        btn_Origin = [[UIButton alloc] initWithFrame:CGRectMake(13, 0, 90, 25)];
        [logoview addSubview:btn_Origin];
        btn_Origin.tag = 1;
        [btn_Origin release];
        [btn_Origin addTarget:self action:@selector(onOptionClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn_Origin setTitle:@"品牌缘起" forState:UIControlStateNormal];
        [btn_Origin.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
        
        btn_Idea = [[UIButton alloc] initWithFrame:CGRectMake(118, 0, 90, 25)];
        [logoview addSubview:btn_Idea];
        btn_Idea.tag = 2;
        [btn_Idea release];
        [btn_Idea addTarget:self action:@selector(onOptionClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn_Idea setTitle:@"品牌理念" forState:UIControlStateNormal];
        [btn_Idea.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
        
        btn_History = [[UIButton alloc] initWithFrame:CGRectMake(223, 0, 90, 25)];
        [logoview addSubview:btn_History];
        btn_History.tag = 3;
        [btn_History release];
        [btn_History addTarget:self action:@selector(onOptionClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn_History setTitle:@"品牌历史" forState:UIControlStateNormal];
        [btn_History.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
        
        btn_Honor = [[UIButton alloc] initWithFrame:CGRectMake(328, 0, 90, 25)];
        [logoview addSubview:btn_Honor];
        btn_Honor.tag = 4;
        [btn_Honor release];
        [btn_Honor addTarget:self action:@selector(onOptionClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn_Honor setTitle:@"荣誉概要" forState:UIControlStateNormal];
        [btn_Honor.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
        [btn_Honor setEnabled:NO];
        
//        btn_Culture = [[UIButton alloc] initWithFrame:CGRectMake(433, 0, 90, 25)];
//        [logoview addSubview:btn_Culture];
//        btn_Culture.tag = 5;
//        [btn_Culture release];
//        [btn_Culture addTarget:self action:@selector(onOptionClick:) forControlEvents:UIControlEventTouchUpInside];
//        [btn_Culture setTitle:@"品牌文化" forState:UIControlStateNormal];
//        [btn_Culture.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        self.rootPath = [NSString stringWithFormat:@"%@/paddata/company/Brand/%@",[paths objectAtIndex:0],[dic objectForKey:@"DocumentName"]];
        
        
        [btn_Origin sendActionsForControlEvents:UIControlEventTouchUpInside];
        
    }
    return self;
}

-(void)onBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)onHome:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)onOptionClick:(UIButton *)sender
{
    [self refreshView];
    
    if (sender.tag == 1) {
        if (brandOriginView == nil) {
            //品牌缘起View
            NSString *originPath = [NSString stringWithFormat:@"%@/Origin",self.rootPath];
            brandOriginView = [[BrandCommonDetailView alloc] initWithFrame:CGRectMake(0, 145, self.view.frame.size.width, self.view.frame.size.height-145) Root:originPath];
        }
        [btn_Origin setTitleColor:[UIColor colorWithRed:0.4  green:0.4  blue:0.4  alpha:1.0] forState:UIControlStateNormal];
        imageLogo.frame = CGRectMake(5, 6, 12, 13);
        [self.view addSubview:brandOriginView];
    }else if(sender.tag == 2){
        if (brandIdeaView==nil) {
            //品牌理念View
            NSString *IdeaPath = [NSString stringWithFormat:@"%@/Idea",self.rootPath];
            brandIdeaView = [[BrandCommonDetailView alloc] initWithFrame:CGRectMake(0, 145, self.view.frame.size.width, self.view.frame.size.height-145) Root:IdeaPath];
        }
        [btn_Idea setTitleColor:[UIColor colorWithRed:0.4  green:0.4  blue:0.4  alpha:1.0] forState:UIControlStateNormal];
        imageLogo.frame = CGRectMake(110, 6, 12, 13);
        [self.view addSubview:brandIdeaView];
    }else if(sender.tag == 3){
        if (brandHistoryView==nil) {
            //品牌历史View
            NSString *historyPath = [NSString stringWithFormat:@"%@/History",self.rootPath];
            brandHistoryView = [[BrandCommonDetailView alloc] initWithFrame:CGRectMake(0, 145, self.view.frame.size.width, self.view.frame.size.height-145) Root:historyPath];
        }
        [btn_History setTitleColor:[UIColor colorWithRed:0.4  green:0.4  blue:0.4  alpha:1.0] forState:UIControlStateNormal];
        imageLogo.frame = CGRectMake(215, 6, 12, 13);
        [self.view addSubview:brandHistoryView];
    }else if(sender.tag == 4){
        //荣誉概要View
        if (nil==honorViewController) {
            NSString* honorPath = [NSString stringWithFormat:@"%@/HonorSummary",self.rootPath];
            honorViewController = [[HonorViewController alloc] initWithFrame:CGRectMake(0, 150, self.view.frame.size.width, self.view.frame.size.height-150) root:honorPath];
            //honorViewController.view.frame = CGRectMake(0, 110, self.view.frame.size.width, self.view.frame.size.height-110);
            [self addChildViewController:honorViewController];
        }

        [btn_Honor setTitleColor:[UIColor colorWithRed:0.4  green:0.4  blue:0.4  alpha:1.0] forState:UIControlStateNormal];
        imageLogo.frame = CGRectMake(320, 6, 12, 13);
        [self.view addSubview:honorViewController.view];
    }
//    else if(sender.tag == 5){
//        if (brandCultureView==nil) {
//            //品牌文化View
//            NSString *culturePath = [NSString stringWithFormat:@"%@/Culture",self.rootPath];
//            brandCultureView = [[BrandCommonDetailView alloc] initWithFrame:CGRectMake(0, 145, self.view.frame.size.width, self.view.frame.size.height-145) Root:culturePath];
//        }
//        [btn_Culture setTitleColor:[UIColor colorWithRed:0.4  green:0.4  blue:0.4  alpha:1.0] forState:UIControlStateNormal];
//        imageLogo.frame = CGRectMake(425, 6, 12, 13);
//        [self.view addSubview:brandCultureView];
//    }
}

-(void) refreshView{
    [btn_Origin setTitleColor:[UIColor colorWithRed:0.6  green:0.6  blue:0.6  alpha:1.0] forState:UIControlStateNormal];
    [btn_Idea setTitleColor:[UIColor colorWithRed:0.6  green:0.6  blue:0.6  alpha:1.0] forState:UIControlStateNormal];
    [btn_History setTitleColor:[UIColor colorWithRed:0.6  green:0.6  blue:0.6  alpha:1.0] forState:UIControlStateNormal];
    [btn_Honor setTitleColor:[UIColor colorWithRed:0.7  green:0.7  blue:0.7  alpha:1.0] forState:UIControlStateNormal];
    //[btn_Culture setTitleColor:[UIColor colorWithRed:0.6  green:0.6  blue:0.6  alpha:1.0] forState:UIControlStateNormal];
    
    if (brandOriginView) {
        if (brandOriginView.superview) {
            [brandOriginView removeFromSuperview];
        }
    }
    
    if (brandIdeaView) {
        if (brandIdeaView.superview) {
            [brandIdeaView removeFromSuperview];
        }
    }
    
    if (brandHistoryView) {
        if (brandHistoryView.superview) {
            [brandHistoryView removeFromSuperview];
        }
    }
    
    if (honorViewController) {
        if (honorViewController.view.superview) {
            [honorViewController.view removeFromSuperview];
        }
    }
    
//    if (brandCultureView) {
//        if (brandCultureView.superview) {
//            [brandCultureView removeFromSuperview];
//        }
//    }
    
}
-(void)dealloc
{
    self.rootPath = nil;
    [super dealloc];
}
@end
